package org.example.springboot;

import org.example.springboot.interceptor.UserAuthInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@EnableAspectJAutoProxy
@ConfigurationPropertiesScan("org.example.springboot.config")
@EnableConfigurationProperties
@SpringBootApplication
@MapperScan("org.example.springboot.mapper")
public class Application implements WebMvcConfigurer {

  @Value("${user.allowed-header}")
  public String X_AUTHORIZATION;

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }


  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new UserAuthInterceptor(X_AUTHORIZATION))
        .addPathPatterns("/user/**", "/article/**")
        .excludePathPatterns("/static/**", "/error");
  }

  @Override
  public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**")
        .allowedOrigins("http://localhost:5173")
        .allowedMethods("GET", "OPTIONS", "PUT", "DELETE",  "POST")
        .allowedHeaders("*")
        .exposedHeaders(X_AUTHORIZATION)
        .allowCredentials(true)
        .maxAge(3600L);
  }
}